import java.util.Arrays;

/**
 * @program: LeetCode
 * @description: LeetCode : 2551. 将珠子放入背包中
 * @author: WXY
 * @create: 2023-01-30 13:32
 * @Version 1.0
 **/
public class Num2551_putMarbles {
    public static long putMarbles(int[] weights, int k) {
        int[] arr = new int[weights.length - 1];
        int index = 0;
        for (int i = 1; i < weights.length; i++) {
            arr[index++] = weights[i - 1] + weights[i];
        }
        Arrays.sort(arr);
        int left = 0;
        int right = arr.length - 1;
        long ans = 0;
        while (--k > 0) {
            ans += arr[right--] - arr[left++];
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {1, 3};
        int k = 2;
        System.out.println(putMarbles(arr, k));
    }
}